package threadLocalByMe;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.ReentrantLock;

public class MyThreadPoolExecutor {
    private volatile boolean RUNNING=true;//线程池状态
    private final ReentrantLock lock=new ReentrantLock();//锁
    private final HashSet<Worker> workers=new HashSet<>();//工作集
    private final ArrayList<Thread> threads=new ArrayList<>();//线程工厂
    private static BlockingQueue<Runnable> queue=null;//任务阻塞队列
    private volatile int poolSize;//线程池的核心线程池
    private volatile int coreSize;//当前线程池中的线程数
    private volatile boolean shutdown=false;//是否停止工作

    public MyThreadPoolExecutor(int poolSize){
        this.poolSize=poolSize;
        queue=new ArrayBlockingQueue<>(poolSize);
    }
    public void execute(Runnable command){
        if(command==null){
            throw new NullPointerException();
        }
        if(coreSize<poolSize) {
            addThread(command);
        }else{
            try{
                queue.put(command);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    private void addThread(Runnable task){
        lock.lock();
        try{
            coreSize++;
            Worker worker=new Worker(task);
            workers.add(worker);
            Thread thread=new Thread(worker);
            threads.add(thread);
            thread.start();
        }finally {
            lock.unlock();
        }
    }
    public void shutdown(){
        RUNNING=false;
        if(!workers.isEmpty()){
            for(Worker worker:workers){
                worker.interruptIfIdle();
            }
        }
        shutdown=true;
        Thread.currentThread().interrupt();
    }
    private final class Worker implements Runnable{

        public Worker(Runnable task){
            queue.offer(task);
        }
        public Runnable getTask() throws InterruptedException {
            return queue.take();
        }
        @Override
        public void run() {
            while(true&&RUNNING){
                if(shutdown){
                    Thread.interrupted();
                }
                Runnable task=null;
                try {
                    task=getTask();
                    task.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public void interruptIfIdle(){
            for(Thread thread:threads){
                System.out.println(thread.getName()+" interrupt");
                thread.interrupt();
            }
        }
    }
}
class Test{
    public static void main(String[] args) {
        MyThreadPoolExecutor executor=new MyThreadPoolExecutor(3);
        for (int i=0;i<10;i++) {
            executor.execute((() -> {
                System.out.println("线程"+Thread.currentThread().getName()+"在工作。。。");
            }));
        }
        executor.shutdown();
    }
}
